package com.gitee.wsl.common.ui.draw.node

import androidx.annotation.FloatRange
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.DrawScope.Companion.DefaultBlendMode
import androidx.compose.ui.graphics.drawscope.DrawStyle
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import com.gitee.wsl.common.ui.base.ColorOrBrush
import com.gitee.wsl.common.ui.base.onBrush
import com.gitee.wsl.common.ui.base.onColor
import com.gitee.wsl.common.ui.draw.api.HasFill
import com.gitee.wsl.common.ui.draw.api.HasStroke
import com.gitee.wsl.common.ui.draw.api.HasTransform
import com.gitee.wsl.common.ui.draw.api.Transform
import com.gitee.wsl.common.ui.draw.style.toStroke
import com.gitee.wsl.mathematics.geometry.shape.Arc


class ArcNode(
    val node: Arc,
) : AbstractNode(),
    HasStroke,
    HasTransform,
    HasFill {

    override var transform: Transform? = null

    override fun DrawScope.drawOutLine(colorOrBrush: ColorOrBrush) {
        val stroke = strokeWidth?.toStroke(this@drawOutLine) ?: Stroke()
        drawArc(
            colorOrBrush = colorOrBrush,
            startAngle = node.startAngle.radian.toFloat(),
            sweepAngle = node.sweepAngle.radian.toFloat(),
            useCenter = node.linkCenter,
            style = stroke
        )
    }

    override fun DrawScope.drawFill(colorOrBrush: ColorOrBrush) {
    }

}

fun DrawScope.drawArc(
    colorOrBrush: ColorOrBrush,
    startAngle: Float,
    sweepAngle: Float,
    useCenter: Boolean,
    topLeft: Offset = Offset.Zero,
    size: Size = this.size.offsetSize(topLeft),
    @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DefaultBlendMode
){
    colorOrBrush.onColor {
        drawArc(it,startAngle, sweepAngle, useCenter, topLeft, size, alpha, style, colorFilter, blendMode)
    }.onBrush {
        drawArc(it,startAngle, sweepAngle, useCenter, topLeft, size, alpha, style, colorFilter, blendMode)
    }
}